#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
//    ui->listWidget->addItem("开心超人");
//    ui->listWidget->addItem("花心超人");
//    ui->listWidget->addItem("甜心超人");
//    ui->listWidget->addItem("小心超人");
//    ui->listWidget->addItem("粗心超人");

//    ui->listWidget->addItem(new QListWidgetItem("国"));
//    ui->listWidget->addItem(new QListWidgetItem("色"));
//    ui->listWidget->addItem(new QListWidgetItem("天"));
//    ui->listWidget->addItem(new QListWidgetItem("香"));
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_insert_clicked()
{
    QString text = ui->lineEdit->text();
    if(text == "")
        return;
//    ui->listWidget->addItem(text);
    ui->listWidget->addItem(new QListWidgetItem(text));
    ui->lineEdit->setText("");
}

void Widget::on_pushButton_erase_2_clicked()
{
    QString text = ui->lineEdit->text();
    if(text == "")
        return;
    // 在选中行的下一行插入
    int pos = ui->listWidget->currentRow();
    if(pos == -1)
        return; // 没有选中，就不插入
    ui->listWidget->insertItem(pos + 1,new QListWidgetItem(text));
    ui->lineEdit->setText("");
}

void Widget::on_pushButton_erase_clicked()
{
    //点击删除当前元素
    int row = ui->listWidget->currentRow();
    if(row == -1)
        return;
    ui->listWidget->takeItem(row);
}
